package com.hgyc.mom.base.print;

import com.hgyc.mom.base.entity.Material;
import com.hgyc.mom.base.printData.MaterialTemplate;
import com.hgyc.mom.base.service.MaterialService;
import com.hgyc.mom.common.constant.CacheConstants;
import com.hgyc.mom.common.print.PrintTemplateDataField;
import com.hgyc.mom.common.print.PrintTemplateDataService;
import com.hgyc.mom.common.redis.RedisCache;
import com.hgyc.mom.common.util.bean.BeanUtils;
import com.hgyc.mom.tool.strategy.AutoCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 物料标签打印数据获取类
 *
 * @author fwj
 * @since 2025/7/17 16:11
 **/
@Service("MaterialLabelService")
public class MaterialPrintTemplateDateService implements PrintTemplateDataService {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private AutoCodeUtil autoCodeUtil;

    @Override
    public List<String> getFields() {
        String key = CacheConstants.PRINT_TEMPLATE_DATA_FIELD_CACHE_KEY + "MaterialLabel";
        List<PrintTemplateDataField> fieldList = redisCache.getCacheList(key,  PrintTemplateDataField.class);
        return fieldList.stream()
                .map(PrintTemplateDataField::getFieldName)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getDesignData() {
        MaterialTemplate mockData = new MaterialTemplate();
        mockData.setBarcode("PN20250101001");
        mockData.setQrcode("PN20250101001");
        mockData.setMaterialId(1001L);
        mockData.setMaterialCode("M-20250715");
        mockData.setRelId("SN-0001");
        mockData.setLotNo("LOT-88");
        mockData.setMaterialName("测试物料");
        mockData.setMaterialSpec("规格A1");
        mockData.setQty(100);
        mockData.setProductDate("2025-07-01 09:00:00");
        Map<String, Object> dataMap = BeanUtils.beanToMap(mockData);
        List<Map<String, Object>> dataMapList = new ArrayList<>();
        dataMapList.add(dataMap);
        return dataMapList;
    }

    @Override
    public List<Map<String, Object>> getDataList(Long bizId,Map<String,String> paramValue,  String codeRule,  Integer qty) {
        Material material = materialService.getById(bizId);

        List<String> codeList = autoCodeUtil.batchGenerateCode(codeRule, paramValue, qty);

        List<MaterialTemplate> materialTemplateList = new ArrayList<>();

        if (!CollectionUtils.isEmpty(codeList)) {
            for (String code : codeList) {
                MaterialTemplate materialTemplate = new MaterialTemplate();
                materialTemplate.setBarcode(code);
                materialTemplate.setQrcode(code);
                materialTemplate.setMaterialId(material.getId());
                materialTemplate.setMaterialCode(material.getMaterialCode());
                materialTemplate.setMaterialName(material.getMaterialName());
                materialTemplate.setMaterialSpec(material.getSpec());
                materialTemplate.setQty(qty);
                if (paramValue != null) {
                    if (paramValue.containsKey("lotNo")) {
                        materialTemplate.setLotNo(paramValue.get("lotNo"));
                    }
                    if (paramValue.containsKey("productDate")) {
                        materialTemplate.setProductDate(paramValue.get("productDate"));
                    }
                }

                materialTemplateList.add(materialTemplate);

            }
        }

        List<Map<String, Object>> dataMapList = BeanUtils.convertMapList(materialTemplateList);


        return dataMapList;
    }
}
